#include "StdAfx.h"
#include "ApprovedExtension.h"

const LPCTSTR CApprovedExtension::RegApprovedExtensions = _T("Software\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved");

bool CApprovedExtension::IsWindowsNT()
{
	OSVERSIONINFO info = { sizeof(OSVERSIONINFO) };

	if (::GetVersionEx(&info))
	{
		return info.dwPlatformId == VER_PLATFORM_WIN32_NT;
	}

	return false;
}

CApprovedExtension::CApprovedExtension(const CComBSTR& name, const CLSID& clsid)
	: m_name(name)
	, m_guid(clsid)
{
}

HRESULT CApprovedExtension::Install() const
{
	if (IsWindowsNT())
	{
		USES_CONVERSION;

		CRegKey key;
		LRESULT result;
		
		if (ERROR_SUCCESS != (result = OpenKey(key)))
		{
			return HRESULT_FROM_WIN32(result);
		}

		if (ERROR_SUCCESS != (result = key.SetStringValue(W2CT(m_name), W2CT(m_guid))))
		{
			return HRESULT_FROM_WIN32(result);
		}
	}

	return S_OK;
}

HRESULT CApprovedExtension::Uninstall() const
{
	if (IsWindowsNT())
	{
		USES_CONVERSION;

		CRegKey key;
		LRESULT result;
		
		if (ERROR_SUCCESS != (result = OpenKey(key)))
		{
			return HRESULT_FROM_WIN32(result);
		}

		if (ERROR_SUCCESS != (result = key.DeleteValue(W2CT(m_name))))
		{
			return HRESULT_FROM_WIN32(result);
		}
	}

	return S_OK;
}

LRESULT CApprovedExtension::OpenKey(CRegKey& key)
{
	return key.Open(HKEY_LOCAL_MACHINE, RegApprovedExtensions, KEY_SET_VALUE);
}